﻿#!/usr/bin/evn python
#coding:utf-8

import os
import sys
import xml.etree.ElementTree as ET

reload(sys)
sys.setdefaultencoding("utf-8")

def GetFileFromDir(dir,ext = None):
    allfiles = []
    needExtFilter = (ext != None)
    for root,dirs,files in os.walk(dir):
        for filespath in files:
            filepath = os.path.join(root, filespath)
            extension = os.path.splitext(filepath)[1][1:]
            if needExtFilter and extension in ext:
                allfiles.append(filepath)
            elif not needExtFilter:
                allfiles.append(filepath)
    return allfiles



# baseURL = "http://10.100.22.169:8080/jenkins/job/zwzx_FuncAuto/lastSuccessfulBuild/artifact/TestResults/"

def GenReport():
    textReport = ""
    reportTemplate = open('../ReportTemplate/ReportTemplate.html')
    try:
        textReport = reportTemplate.read( )
    finally:
        reportTemplate.close( )

    i = 0
    allfiles = GetFileFromDir("../TestResults","jtl")
    for sfile in allfiles:

        i = i + 1
        passNum = 0
        failNum = 0
        totalNum = 0

        try:
            tree = ET.parse(sfile)
            root = tree.getroot()
        except Exception, e:
            print "Error:cannot parse file: " + sfile
            sys.exit(1)

        newTestCasePassFlag = True
        newTestCaseName = ""
        j = 0
        for child in root:
            lbValue = child.get('lb')[0:11]
            sValue = child.get('s')

            if j == 0:
                newTestCaseName = lbValue

            if sValue == "false":
                newTestCasePassFlag = False

            if ( newTestCaseName != lbValue):
                if newTestCasePassFlag == False:
                    failNum += 1
                # else:
                #     passNum += 1

                totalNum += 1

                newTestCaseName = lbValue
                newTestCasePassFlag = True

            j += 1
        totalNum += 1
        passNum = totalNum - failNum

        passRate = round(float(passNum)/float(totalNum)*100, 2)
        # print passRate

        fileName = os.path.basename(sfile).split(".")[0]
        textReport +=  "<tr>\n"
        textReport +=  "<td>" + str(i) + "</td>\n"
        textReport +=  "<td>" + fileName + "</td>\n"

        result = ""
        if(passNum == totalNum):
            result = "PASS"
            textReport +=  "<td style='color:green'>" + result + "</td>\n"
        else:
            result = "FAIL"
            textReport +=  "<td style='color:red'>" + result + "</td>\n"

        textReport +=  "<td>" + str(totalNum) + "</td>\n"

        if(failNum != 0):
            textReport +=  "<td style='color:red'>" + str(failNum) + "</td>\n"
            textReport +=  "<td style='color:red'>" + str(passRate) + "%</td>\n"
        else:
            textReport +=  "<td>0</td>\n"
            textReport +=  "<td>100%</td>\n"

        textReport +=  "<td><a href='" + baseURL + fileName + ".html" + "' target='view_window'>详细数据</a>" + "</td>\n"
        textReport +=  "</tr>\n"

    textReport +=  "</table>\n</body>\n</html>"
    output = open('../TestResults/Summary.html', 'w')
    output.write(textReport)
    output.close()



if __name__=='__main__':
    baseURL = sys.argv[1]
    print "baseURL: " + baseURL
    GenReport()
